<?php
namespace App\Services;

use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Redis;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Illuminate\Support\Facades\Http;

class BaoJianOrderService {
    /**
     * @notes 发起公共请求
     * @param $path
     * @param $data
     * @param $header
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/06/06 15:33
     */
    private static function commonRequest($path, $data, $needToken = true)
    {
        $host = config('services.bj.orderHost');

        $url = $host . $path;

        $header = [];

        if($needToken) {
            $header['v-token'] = self::getToken();
        }

        $response = Http::retry(3, 1000)
            -> timeout(3)
            -> withHeaders($header)
            -> get($url, $data)
            -> throw()
            -> json();

        if(!$response) {
            throw new AccessDeniedHttpException('宝坚服务请求失败');
        }

        if($response['retCode'] !== 200) {
            throw new AccessDeniedHttpException($response['retMsg']);
        }

        return $response;
    }

    private static function getToken()
    {
        $token = Redis::get('bj:request_token');

        if($token) {
            return $token;
        }

        $data = [
            'appId'     => config('services.bj.orderAppId'),
            'appSecret' => config('services.bj.orderAppKey'),
        ];

        $path = '/openapi/get_token';

        $res = self::commonRequest($path, $data, false);

        Redis::setex('bj:request_token', 60*60, $res['data']['token']);

        return $res['data']['token'];
    }

    public static function getOrderList($params, $list = [])
    {
        $data = [
            "page"           => 1,
            "limit"          => 100,
            "deviceCode"     => $params['deviceCode'] ?? '',
            "beginPayTime"   => $params['beginPayTime'] ?? '',
            "endPayTime"     => $params['endPayTime'] ?? '',
        ];

        $path = "/openapi/page_order";

        $res = self::commonRequest($path, $data);

        $order_list = array_merge($res['data']['rows'], $list);

        if(count($list) < $res['data']['total']) {
            $data['page'] += 1;
            return self::getOrderList($data, $order_list);
        }

        return $list;
    }

    public static function getRefundOrderList($params, $list = [])
    {
        $data = [
            "page"           => 1,
            "limit"          => 100,
            "deviceCode"     => $params['deviceCode'] ?? '',
            "beginRefundTime"   => $params['beginPayTime'] ?? '',
            "endRefundTime"     => $params['endPayTime'] ?? '',
        ];

        $path = "/openapi/page_refund";

        $res = self::commonRequest($path, $data);

        $order_list = array_merge($res['data']['rows'], $list);

        if(count($list) < $res['data']['total']) {
            $data['page'] += 1;
            return self::getRefundOrderList($data, $order_list);
        }

        return $list;
    }
}
